package cn.zhku.edu.orderservice.service.impl;

import cn.zhku.edu.orderservice.mapper.OrderMapper;
import cn.zhku.edu.feign.pojo.bo.Orderdetail;
import cn.zhku.edu.feign.pojo.entry.Order;
import cn.zhku.edu.orderservice.service.OrderServies;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author clk
 * @Date:2021/9/13  14:17
 **/
@Service
public class OrderServiesImpl extends ServiceImpl<OrderMapper, Order> implements OrderServies {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public int deleteByPrimaryKey(Order order) {
        return orderMapper.deleteByPrimaryKey(order);
    }

    @Override
    public int insert(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public int insertdetail(Orderdetail order) {
        return orderMapper.insertdetail(order);
    }

    @Override
    public int insertSelective(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public List<Order> selectByPrimaryKey(String uid) {
        return orderMapper.selectByPrimaryKey(uid);
    }

    @Override
    public List<List<Orderdetail>> selectAllOrder(String uid) {
        List<Orderdetail> allorder =orderMapper.selectAllOrder(uid);
        List<List<Orderdetail>> order=new ArrayList<>();
        List<Orderdetail> goodsList=new ArrayList<>();

        Integer oldoid = null;
        Integer newoid;
        for(int i=0;i<allorder.size();i++){
            Orderdetail item=allorder.get(i);
            newoid=allorder.get(i).getOid();
            if(oldoid==null){
                goodsList.add(item);
                oldoid=newoid;
            }else if(oldoid.equals(newoid)){
                goodsList.add(item);
                oldoid=newoid;

            }else {
                order.add(goodsList);
                goodsList=new ArrayList<>();
                goodsList.add(item);
                oldoid=newoid;
            }

            if((i+1)==allorder.size()){
                order.add(goodsList);
            }

        }
        return order;
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        return orderMapper.updateByPrimaryKey(record);
    }
}
